# -*- ecoding: utf-8 -*-
# @ModuleName: dataStore
# @Author: Kandy
# @Time: 2023-08-12 21:09
from PIL import Image
import io

class GlobalVar:
    teacher_status = []
    detectionRes = [] # 存储算法检测每一帧的L、NL、stand...，里面装的是字典
    list_len = 1 * 30 * 60 * 45 # 1帧 * 30 = 1s ，1s * 60 = 1分 ，1分 * 45 = 45分 一节课
    # S:教学质量综合指标；Sa:课堂出勤率；Sb:课堂参与度分析；Sv:学生课堂活跃度；课堂专注度:Sf；Sm:教师课堂活跃度
    S = Sa = Sb = Sv = Sf = Sm = ""

    # 重写append方法
    def append(self,data):
        # data为当前帧检测的L、NL
        # 用于判断列表缓存多少帧的处理信息
        if self.detectionRes.__len__() >= self.list_len:
            self.detectionRes.pop(0)
        self.detectionRes.append(data)

    # 统计当前帧检测到的人数
    def numCount(self,detectionRes_frame):
        detect_person_num = 0 # 初使人数为0
        for action in detectionRes_frame:
            if action == "L" or action == "NL":
                detect_person_num = detect_person_num + 1
            else:
                continue
        return detect_person_num

    # 给Si赋值
    def changeValue(self,str):
        if len(str) == 0:
            # 表明字符串为空
            str = 0
        else:
            str = float(str)
        return str

    def byte_img_save(self,byte_data,frame_num):
        root_path = r"D:\desktop\ketangEnd\ketang_end\flask_ketang_huizong_xiugai_mulgpu\byte_img_save"
        # 创建一个BytesIO对象，并将字节流写入其中
        stream = io.BytesIO(next(iter(byte_data.values())))
        # 使用PIL库打开BytesIO对象，并将其转换为图片对象
        image = Image.open(stream)
        # 保存图片到本地文件
        image.save(root_path + "\\image_" + str(frame_num) + ".jpg")

    def actionNum(self):
        actions = self.detectionRes[-1]["detectRes"]
        currentFrame = self.detectionRes[-1]["frameNum"]
        L_num = NL_num = stand_num = hand_num = sleep_num = 0
        for action in actions:
            if action == "L":
                L_num += 1
            elif action == "NL":
                NL_num += 1
            elif action == "stand":
                stand_num += 1
            elif action == "sleep":
                sleep_num += 1
            else:
                hand_num += 1

        actions_num = {currentFrame:{"L":L_num,"NL":NL_num,"stand":stand_num,"hand":hand_num,"sleep":sleep_num}}
        return actions_num



